﻿namespace CSharpCompiler
{
    using Mono.CSharp;
    using System;
    using System.CodeDom.Compiler;
    using System.Runtime.CompilerServices;

    public class CustomReportPrinter : ReportPrinter
    {
        private readonly CompilerResults compilerResults;

        public CustomReportPrinter(CompilerResults compilerResults)
        {
            this.compilerResults = compilerResults;
        }

        public override void Print(AbstractMessage msg, bool showFullPath)
        {
            if (msg.IsWarning)
            {
                this.WarningsCount++;
            }
            else
            {
                this.ErrorsCount++;
            }
            CompilerError error = new CompilerError {
                IsWarning = msg.IsWarning,
                Column = msg.Location.Column,
                Line = msg.Location.Row,
                ErrorNumber = msg.Code.ToString(),
                ErrorText = msg.Text,
                FileName = !showFullPath ? msg.Location.SourceFile.Name : msg.Location.SourceFile.FullPathName
            };
            this.compilerResults.Errors.Add(error);
        }

        public int ErrorsCount { get; protected set; }

        public int WarningsCount { get; private set; }
    }
}

